//
//  AppDelegate.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/10/16.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "WelcomeViewController.h"
#import "AccountViewModel.h"
#import "WelcomeViewController.h"
#import "ComposeViewController.h"
#import "BaseNavViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//注册通知
- (void)registerNotification;{

    NSLog(@"我是first2");

    NSLog(@"我是first1");

    //    object 观察是哪个对象发出的通知,如果是nil,则表示可以接受所有对象发出的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AppRootViewController:) name:KChoseRootVC object:nil];
}

- (void)AppRootViewController:(NSNotification *)n{
    
    MainViewController *maincontr = [[MainViewController alloc]init];
    WelcomeViewController *welcomecontr = [[WelcomeViewController alloc]init];
    
    //通知是同步的,是不会单独在开线程的
    NSLog(@"come here delegate");
    if (n.object != nil){
        //根视图控制器是tabbarVC
        _window.rootViewController = maincontr;
    } else {
        //根视图控制器是欢迎页面
        _window.rootViewController = welcomecontr;
    }
}

//懒加载
- (UIViewController *)defaultRootViewController{
    
    if ([AccountViewModel sharedAccountModel].userLogin) {
        
        WelcomeViewController *welcome = [[WelcomeViewController alloc]init];
        
        return welcome;
    }
    
    MainViewController *maincontroller = [[MainViewController alloc]init];
    
    
    return maincontroller;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSLog(@"%d",[AccountViewModel sharedAccountModel].userLogin);
    NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, true) lastObject]);
//    点击空白处键盘消失的操作
//    http://www.jianshu.com/writer#/notebooks/3690459/notes/7659016/preview
    [self openTouchOutsideDismissKeyboard];
    
    [self registerNotification];
    
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    _window.backgroundColor = [UIColor whiteColor];
    
    
    [_window makeKeyAndVisible];;
    
        _window.rootViewController = self.defaultRootViewController;
    
//    PictureSelectorViewController *compose = [[PictureSelectorViewController alloc]init];
//    BaseNavViewController *basecontroller = [[BaseNavViewController alloc]initWithRootViewController:compose];
//    _window.rootViewController = basecontroller;
    [UINavigationBar appearance].tintColor = themColor;
    
    [UITabBar appearance].tintColor = themColor;
    
    return YES;
}

/** 开启点击空白处隐藏键盘功能 */
- (void)openTouchOutsideDismissKeyboard
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addGesture) name:UIKeyboardDidShowNotification object:nil];
}
- (void)addGesture
{
    [self.window addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disappearKeyboard)]];
}
- (void)disappearKeyboard
{
    [self.window endEditing:YES];
    [self.window removeGestureRecognizer:self.window.gestureRecognizers.lastObject];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


@end
